﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace theguineasyndrome
{
    public class PortalWeapon : Weapon
    {
        private Texture2D blueDropTexture;
        private Texture2D orangeDropTexture;

        private Texture2D bluePortalTexture;
        private Texture2D orangePortalTexture;

        private PortalDrop blueDrop;
        private PortalDrop orangeDrop;

        private Portal bluePortal;
        private Portal orangePortal;

        private SoundEffect sound;

        public PortalWeapon(Texture2D blueDropTexture, Texture2D orangeDropTexture, Texture2D bluePortalTexture, Texture2D orangePortalTexture, SoundEffect sound)
        {
            this.blueDropTexture = blueDropTexture;
            this.orangeDropTexture = orangeDropTexture;

            this.bluePortalTexture = bluePortalTexture;
            this.orangePortalTexture = orangePortalTexture;

            this.sound = sound;
        }

        public override Vector2 firePrimary(int time, Vector2 position, Vector2 speed)
        {
            sound.Play(0.3f, 0, 0);
            firePortal(position, speed, blueDropTexture, ref blueDrop, Portal.Color.BLUE);
            return new Vector2();
        }

        public override Vector2 fireSecondary(int time, Vector2 position, Vector2 speed)
        {
            sound.Play(0.3f, 0, 0);
            firePortal(position, speed, orangeDropTexture, ref orangeDrop, Portal.Color.ORANGE);
            return new Vector2();
        }

        private void firePortal(Vector2 position, Vector2 speed, Texture2D dropTexture, ref PortalDrop drop, Portal.Color color)
        {
            speed.Normalize();
            speed *= 25;
            drop = new PortalDrop(level, dropTexture, position, speed, color);
        }

        public override float getPrimaryPeriod()
        {
            return 0;
        }

        public override float getSecondaryPeriod()
        {
            return 0;
        }

        public override void Update(GameTime gameTime)
        {
            UpdatePortalDrop(gameTime, ref blueDrop, ref bluePortal, orangePortal, blueDropTexture, bluePortalTexture);
            UpdatePortalDrop(gameTime, ref orangeDrop, ref orangePortal, bluePortal, orangeDropTexture, orangePortalTexture);

            if (bluePortal != null)
                bluePortal.Update(gameTime);

            if (orangePortal != null)
                orangePortal.Update(gameTime);
        }

        private void UpdatePortalDrop(GameTime gameTime, ref PortalDrop drop, ref Portal portal, Portal pairPortal, Texture2D dropTexture, Texture2D portalTexture)
        {
            if (drop != null)
            {
                drop.Update(gameTime);
                if (drop.isDead())
                {
                    ObstacleElement killingObstacle = drop.getKillingObstacle();
                    if (killingObstacle is PortalableElement)
                    {
                        Portal oldPortal = portal;

                        portal = new Portal(oldPortal, portalTexture, drop);

                        if (portal.isValid())
                        {
                            if (oldPortal != null)
                                oldPortal.removeFromElement();

                            portal.setPairPortal(pairPortal);

                            if (pairPortal != null)
                                pairPortal.setPairPortal(portal);
                        }
                        else
                        {
                            portal.removeFromElement();
                            portal = oldPortal;
                        }
                    }
                    drop = null;
                }
            }
        }

        public override void queueDrawing(Camera camera)
        {
            if (blueDrop != null)
                blueDrop.queueDrawing(camera);

            if (orangeDrop != null)
                orangeDrop.queueDrawing(camera);
        }

        public override bool primaryRequiresJustClicked()
        {
            return true;
        }

        public override bool secondaryRequiresJustClicked()
        {
            return true;
        }

        public override void clear()
        {
            blueDrop = null;
            orangeDrop = null;
            bluePortal = null;
            orangePortal = null;
        }
    }
}
